package Servlet;

import com.alibaba.fastjson.JSONArray;
import dao.MyConnection;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class complainServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-cache");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        MyConnection myCon = new MyConnection();
        myCon.getConnection();
        JSONArray a = new JSONArray();

        try {
            PreparedStatement ps = myCon.connection.prepareStatement("select * from complaint;");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                JSONObject j = new JSONObject();
                j.put("id", rs.getString(1));
                j.put("sa", rs.getString(2));
                j.put("cc", rs.getString(3));
                j.put("cr", rs.getString(4));
                a.add(j);
            }
            ps.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myCon.closeConnection();
        }
        out.print(a.toJSONString());
    }
}
